package direct;

import com.rabbitmq.client.*;
import constant.Constant;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ConsumerDirect1 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setPort(Constant.PORT);
        connectionFactory.setVirtualHost(Constant.VIRTUAL_HOST);
        connectionFactory.setUsername(Constant.USER_NAME);
        connectionFactory.setPassword(Constant.PASSWORD);
        connectionFactory.setHost(Constant.HOST);
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        //已经声明过队列,这里就不再声明队列了
        DefaultConsumer defaultConsumer =new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收到消息:"+new String(body));
            }
        };
        channel.basicConsume(Constant.DIRECT_QUEUE_NAME_1, true, defaultConsumer);

    }
}
